package com.xw.childhealth.controller;

import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.ChildrenDxzsk;
import com.xw.childhealth.service.ChildrenDxzskService;
import com.xw.childhealth.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-14 14:50
 * @Version: 1.0
 */
@RestController
@RequestMapping("/childrenDxzsk")
public class ChildrenDxzskController {

    @Autowired
    private ChildrenDxzskService childrenDxzskService;

    @PostMapping("/findChildrenDxzskParent")
    public ResultVO findChildrenDxzskParent() {
        List<ChildrenDxzsk> result = childrenDxzskService.findByParentIdEquals(0);
        return ResultVOUtil.success(result);
    }


    /**
     * 查询下拉列表详细内容
     *
     * @param id
     * @return
     */
    @PostMapping("/getChildrenStandardDetail/{id}")
    public ResultVO findDetail(@PathVariable("id") Integer id) {

        List<ChildrenDxzsk> result = childrenDxzskService.findByParentIdEquals(id);
        Collections.sort(result);
        result.sort(new Comparator<ChildrenDxzsk>() {
            @Override
            public int compare(ChildrenDxzsk o1, ChildrenDxzsk o2) {
                return o1.getMonth() - o2.getMonth();
            }
        });
        return ResultVOUtil.success(result);
    }


    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type,
                             @RequestParam(value = "id", required = false) Integer id,
                             @RequestParam(value = "year", required = false) Integer year,
                             @RequestParam(value = "month", required = false) Integer month,
                             @RequestParam(value = "monthAge", required = false) Integer monthAge,
                             @RequestParam(value = "nodeId", required = false) Integer nodeId,
                             @RequestParam(value = "isOpen", required = false) String isOpen,
                             @RequestParam(value = "orderNumber", required = false) Integer orderNumber,
                             @RequestParam(value = "detail", required = false) String detail,
                             @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode,
                             Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/children-dxzsk", map);
        }
        if (type.equals("toUpd")) {
            ChildrenDxzsk one = childrenDxzskService.findByIdEquals(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            ChildrenDxzsk one = childrenDxzskService.findByIdEquals(nodeId);
            one.setUpdateTime(new Date());
            one.setDetail(detail);
            one.setYear(year);
            one.setMonth(month);
            one.setMonthAge(monthAge);
            one.setOrderNumber(orderNumber);
            one.setIsOpen(isOpen == null ? 0 : 1);
            childrenDxzskService.save(one);
            return new ModelAndView("/pages/admin/children-dxzsk", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            childrenDxzskService.save(new ChildrenDxzsk().setYear(year).setMonth(month).setMonthAge(monthAge).setIsOpen(isOpen == null ? 0 : 1).setDetail(detail).setParentId(addRecordNode).setOrderNumber(orderNumber).setCreateTime(new Date()).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/children-dxzsk", map);
        }
        return new ModelAndView("/pages/admin/children-dxzsk-data-add", map);
    }


    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        ChildrenDxzsk result = childrenDxzskService.findByIdEquals(id);
        if (result != null) {
            childrenDxzskService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/children-dxzsk");
    }
}
